import Cookies from 'js-cookie'
import store from '@/store'
import md5 from 'js-md5'
import Vue from 'vue';
//2.新创建一个vue实例
let v = new Vue();

/**
 * 权限
 * @param {*} key
 */
export function hasPermission (key) {
  return window.SITE_CONFIG['permissions'].indexOf(key) !== -1 || false
}

/**
 * 获取字典数据列表
 * @param dictType  字典类型
 */
export function getDictDataList (dictType,paramType) {
	var tt = 'dictList'
  if( paramType === '2' ){
	  tt = 'bizDictList';
  }
  const type = window.SITE_CONFIG[tt].find((element) => (element.paramCode === dictType))
  if (type) {
    return type.dataList
  } else {
    return []
  }
}

/**
 * 获取字典名称
 * @param dictType  字典类型
 * @param dictValue  字典值
 */
export function getDictLabel (dictType, dictValue,pt) {
	var tt = 'dictList'
	if( pt === '2' ){
		  tt = 'bizDictList';
	}
  const type = window.SITE_CONFIG[tt].find((element) => (element.paramCode === dictType))
  if (type) {
    const val = type.dataList.find((element) => (element.paramValue === dictValue + ''))
    if (val) {
      return val.paramKey
    } else {
      return dictValue
    }
  } else {
    return dictValue
  }
}

export function getDictByLabel (dictType, dictValue,pt) {
	console.log(dictType,dictValue,pt)
	var tt = 'dictList'
	if( pt === '2' ){
		tt = 'bizDictList';
	}
  const type = window.SITE_CONFIG[tt].find((element) => (element.paramCode === dictType))
  if (type) {
    const val = type.dataList.find((element) => (element.paramValue === dictValue + ''))
    if (val) {
      return val
    } else {
      return {}
    }
  } else {
    return {}
  }
}

/**
 * 清除登录信息
 */
export function clearLoginInfo () {
  store.commit('resetStore')
  Cookies.remove('token')
  Cookies.remove('userImg')
  localStorage.removeItem('current_permissions');
  localStorage.removeItem('current_user');
  window.SITE_CONFIG['dynamicMenuRoutesHasAdded'] = false
}

/**
 * 获取uuid
 */
export function getUUID () {
  return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, c => {
    return (c === 'x' ? (Math.random() * 16 | 0) : ('r&0x3' | '0x8')).toString(16)
  })
}

export function setMd5 (content) {
  return md5(content);
}

/**
 * 获取svg图标(id)列表
 */
export function getIconList () {
  var res = []
  var list = document.querySelectorAll('svg symbol')
  for (var i = 0; i < list.length; i++) {
    res.push(list[i].id)
  }

  return res
}

/**
 * 树形数据转换
 * @param {*} data
 * @param {*} id
 * @param {*} pid
 */
export function treeDataTranslate (data, id = 'id', pid = 'pid') {
  var res = []
  var temp = {}
  for (var i = 0; i < data.length; i++) {
    temp[data[i][id]] = data[i]
  }
  for (var k = 0; k < data.length; k++) {
    if (!temp[data[k][pid]] || data[k][id] === data[k][pid]) {
      res.push(data[k])
      continue
    }
    if (!temp[data[k][pid]]['children']) {
      temp[data[k][pid]]['children'] = []
    }
    temp[data[k][pid]]['children'].push(data[k])
    data[k]['_level'] = (temp[data[k][pid]]._level || 0) + 1
  }
  return res
}

/**
 * @param {Object} textToCopy 需要复制的文本
 * @param {Object} isShowText	是否显示弹框 不传则不显示任何提醒 0-提示，但不显示复制的文本 1-提示，显示复制的文本
 */
export function copyText(textToCopy,isShowText){
	// 创建text area
	let textArea = document.createElement("textarea");
	textArea.value = textToCopy;
	// 使text area不在viewport，同时设置不可见
	textArea.style.position = "absolute";
	textArea.style.opacity = 0;
	textArea.style.left = "-999999px";
	textArea.style.top = "-999999px";
	document.body.appendChild(textArea);
	textArea.focus();
	textArea.select();
	return new Promise((res, rej) => {
		// 执行复制命令并移除文本框
		if( document.execCommand('copy') ){
			if( isShowText === true ){
				v.$message({
				  dangerouslyUseHTMLString: true,
				  type: 'success',
				  message: '  复制成功 <br><br>' + '  ' + textToCopy
				});
			}else if( isShowText === false ){
				v.$message.success('复制成功~')
			}
			res()
		}else{
			v.$message.error('复制失败!')
			rej()
		}
		// document.execCommand('copy') ? res() : rej();
		textArea.remove();
	});
}
